package com.menggudf.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.menggudf.common.utils.EncodeExcelFileNameUtils;
import com.menggudf.common.utils.ExcelUtils;
import com.menggudf.model.ExportIllnessModel;
import com.menggudf.model.IllnessModel;
import com.menggudf.model.ResultModel;
import com.menggudf.model.SystemAdminModel;
import com.menggudf.service.DiagnosisService;
import com.menggudf.service.IllnessService;
import com.menggudf.service.SystemAdminService;

@RequestMapping("/adminIllness")
@Controller
public class AdminIllnessController {
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(AdminIllnessController.class);
    
    @Autowired
    private IllnessService illnessService;
    
    @Autowired
    private SystemAdminService systemAdminService;
    
    @Autowired
    private DiagnosisService diagnosisService;
    
    /**
     * 获取疾病列表
     * */
    @RequestMapping("getIllnessList")
    @ResponseBody
    public Map<String, Object> getIllnessList(
            @RequestParam(value="illness",required=false) String illName,
            @RequestParam(value="symptom",required=false) String symptomName,
            @RequestParam(value="parts",required=false) String parts,
            @RequestParam(value="levelOneSubject",required=false) String levelOneSubject,
            @RequestParam(value="levelTwoSubject",required=false) String levelTwoSubject,
            @RequestParam(value="startTime",required=false) Date startTime,
            @RequestParam(value="endTime",required=false) Date endTime,
            @RequestParam(value="age",required=false) String age,
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            HttpSession httpSession){
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model==null){
            return new HashMap<String, Object>();
        }
        return illnessService.getIllnessList(
                illName,
                symptomName,
                parts,
                levelOneSubject,
                levelTwoSubject,
                startTime,
                endTime,
                age,
                pageSize,
                offset,model.getAdminCode());
        
    }
    
    @RequestMapping("getIllnessDetail")
    @ResponseBody
    public IllnessModel getIllnessDetail(
            @RequestParam(value="illness") String illness){
        return illnessService.getIllnessDetail(illness);
        
    }
    
    /**
     * 编辑疾病记录
     * */
    @RequestMapping("editIllnessRecort")
    @ResponseBody
    public ResultModel editIllnessRecort(
            @RequestParam(value="illness") String illness,
            @RequestParam(value="symptoms") String symptoms,
            @RequestParam(value="isInjured") Boolean isInjured,
            @RequestParam(value="duration") String duration,
            @RequestParam(value="parts") String parts,
            @RequestParam(value="specialCrowd") String specialCrowd,
            @RequestParam(value="levelOneSubject") String levelOneSubject,
            @RequestParam(value="levelTwoSubject") String levelTwoSubject,
            @RequestParam(value="diseaseAge",required=false) String diseaseAge,
            @RequestParam(value="examNames",required=false) String examNames,
            @RequestParam("pathogen") String pathogen,
            @RequestParam("scheme") String scheme,
            @RequestParam("medicine") String medicine,
            HttpSession httpSession) {
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model==null || model.getAdminCode()==null || model.getAdminName()==null){
            
            return ResultModel.fail();
        }
        return illnessService.editIllnessRecort(
                illness,
                symptoms,
                parts,
                specialCrowd,
                levelOneSubject,
                levelTwoSubject,
                diseaseAge,
                model.getAdminCode(),
                examNames,
                isInjured,
                duration,
                pathogen,
                scheme,
                medicine);

    }
    
    /**
     * 编辑疾病记录
     * */
    @RequestMapping("editIllness")
    @ResponseBody
    public ResultModel editIllness(
            @RequestParam(value="illness") String illness,
            @RequestParam(value="duration") String duration,
            @RequestParam(value="sex") Integer sex,
            @RequestParam(value="levelOneSubject") String levelOneSubject,
            @RequestParam(value="levelTwoSubject") String levelTwoSubject,
            @RequestParam(value="assistantExam",required=false) String assistantExam,
            @RequestParam("pathogen") String pathogen,
            @RequestParam("scheme") String scheme,
            @RequestParam("medicine") String medicine) {
        return illnessService.editIllness(
                illness,
                sex,
                levelOneSubject,
                levelTwoSubject,
                assistantExam,
                duration,
                pathogen,
                scheme,
                medicine);

    }
    
    /**
     * 编辑疾病病症
     * */
    @RequestMapping("editSym")
    @ResponseBody
    public ResultModel editSym(
            @RequestParam(value="illness") String illness,
            @RequestParam("symptom") String symptom,
            
            @RequestParam("parts") String parts,
            @RequestParam("description") String description,
            
            @RequestParam(value="degree",required=false) String degree,
            @RequestParam(value="nature",required=false) String nature,
            @RequestParam(value="times",required=false) String times,
            @RequestParam(value="medicine",required=false) String medicine,
            @RequestParam(value="sdown",required=false) Double sdown,
            @RequestParam(value="sup",required=false) Double sup,
            @RequestParam(value="imgurl",required=false) String imgurl,
            @RequestParam(value="id",required=false) Integer id) {
        return illnessService.editSym(
                illness,
                symptom,
                degree,
                nature,
                times,
                medicine,
                sdown,
                sup,
                imgurl,
                id,parts,description);

    }
    
    /**
     * 编辑疾病部位
     * */
    @RequestMapping("editPart")
    @ResponseBody
    public ResultModel editPart(
            @RequestParam(value="illness") String illness,
            @RequestParam(value="part") String part,
            @RequestParam(value="id") Integer id) {
        return illnessService.editPart(illness,part,id);

    }
    
    /**
     * 创建疾病
     * */
    @RequestMapping("/addIll")
    @ResponseBody
    public ResultModel addIll(@RequestBody IllnessModel illnessModel){
        return illnessService.addIll(illnessModel);
        
    }

    /**
     * 疾病导出
     * */
    @RequestMapping("/exportIllness")
    @ResponseBody
    public void exportIllness(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="illness") String illness){
        List<ExportIllnessModel> list = illnessService.exportIllness(illness);
        OutputStream out = null;
        try{
            response.setHeader("Connection", "close");
            response.setHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
            String filename = EncodeExcelFileNameUtils.encodingFileName(request, list.get(0).getIllness() +".xlsx");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            out = response.getOutputStream();
            ExcelUtils.exportExcel(out, list.get(0).getIllness(), list, ExportIllnessModel.class);
        } catch (Exception e) {
            LOGGER.error("export ExportIllnessModel failed", e);
        } finally {
            if (out !=null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    LOGGER.error("exist ExportIllnessModel OutputStream failed", e1);
                }
            }
        }
    }
    
    /**
     * 获取常见病症列表
     * */
    @RequestMapping("/getSymptomList")
    @ResponseBody
    public Map<String, Object> getSymptomList(
            @RequestParam(value="symptom",required=false) String symptom,
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset){
        return illnessService.getSymptomList(symptom,pageSize,offset);
        
    }
    
    /**
     * 设置常见
     * */
    @RequestMapping("/setSymptomCommon")
    @ResponseBody
    public ResultModel setSymptomCommon(
            @RequestParam(value="symptom") String symptom,
            HttpSession httpSession){
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model==null){
            return ResultModel.fail("登录超时", null);
        }
        return illnessService.setSymptomCommon(symptom,model.getAdminName());
        
    }
    
    /**
     * 设置非常见
     * */
    @RequestMapping("/setSymptomCommonNot")
    @ResponseBody
    public ResultModel setSymptomCommonNot(
            @RequestParam(value="symptom") String symptom){
        return illnessService.setSymptomCommonNot(symptom);
        
    }
    
    @RequestMapping("/synAllIllMedicines")
    @ResponseBody
    public ResultModel synAllIllMedicines(
            HttpSession httpSession){
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model!=null){
            if(model.getFlag()!=1){
                return ResultModel.fail("非管理员",null);
            }
        }else{
            return ResultModel.fail("登录超时", null);
        }
        return illnessService.synAllIllMedicines();
        
    }
}
